/**
 * 
 */
package com.zhiteng.controller;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zhiteng.entity.Teacher;
import com.zhiteng.service.TeacherService;
import com.zhiteng.utils.common.ZTResult;

/**  
* <p>Title: TeacherController.java</p>  
* <p>Description: 教师Controller</p>  
* @author dragon  
* @date 2018年12月28日  
* @version 1.0  
*/
@RequestMapping("/teacher")
@RestController
public class TeacherController {
	@Autowired
	private TeacherService teacherService;
	
	/**
	 * 
	* @Title: queryBonusPoint  
	* @Description: 根据教师id查询教师的积分 
	* @param teacherId	教师id
	* @return ZTResult<Integer>   
	* @throws
	 */
	@RequestMapping("/queryBonusPoint")
	public ZTResult<Integer> queryBonusPoint(@RequestParam(name="teacherId")String teacherId) {
		Teacher teacher = teacherService.queryTeacherById(teacherId);
		if(teacher==null) return ZTResult.result(1, "未查询到该教师");
		return ZTResult.result(0, "查询成功",teacher.getBonusPoint());
	}
	
	/**
	 * 
	* @Title: addBonusPoint  
	* @Description: 修改教师积分 
	* @param teacherId	教师id
	* @param type	加积分的类型：1-请假  2-调代课   3-发布成绩  4-发布作业  5-群发消息
	* @param timestamp	时间戳
	* @param sign		签名
	* @return ZTResult<Void>   
	* @throws
	 */
	@RequestMapping("/updateBonusPoint")
	public ZTResult<Void> updateBonusPoint(@RequestParam(name="teacherId")String teacherId,
			@RequestParam(name="type")Integer type,
			@RequestParam(name="timestamp")Long timestamp,
			@RequestParam(name="sign")String sign) {
		//1、进行接口安全校验
		//	约定sign生成方式：key+timestamp进行MD5加密转为大写生成
		//		--	这里约定key为预先准备好的UUID：13a27ab62b6c47e7b363a91721818b8c
		if(System.currentTimeMillis()-timestamp>6000) {	//	相差超过6秒，则验证不通过
			return ZTResult.result(1, "访问超时");
		}
		//	进行MD5加密（生成签名）
		String result = DigestUtils.md5Hex(timestamp.toString()+"13a27ab62b6c47e7b363a91721818b8c").toUpperCase();
		if(!result.equals(sign)) {
			return ZTResult.result(1, "签名错误");
		}
		
		//2、校验通过，修改教师积分
		int count = teacherService.updateBonusPoint(teacherId,type);
		if(count==-2) return ZTResult.result(1, "type不存在");
		if(count==-1) return ZTResult.result(1, "未查询到该教师");
		if(count==-3) return ZTResult.result(1, "周期内已达积分上限");
		if(count==1) return ZTResult.result(0, "修改成功");
		return ZTResult.result(1, "修改失败");
	}

}
